package com.example.jussi.viewpagerdemo;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ViewPager viewPager;
    LinearLayout layout1, layout2, layout3, layout4;
    ImageView imageView1, imageView2, imageView3, imageView4;
    TextView textView1, textView2, textView3, textView4;
    List<View> list;
    ImageView currentImageView;
    TextView currentTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }
        initView();
        initData();
    }

    private void initView(){
        viewPager = (ViewPager) findViewById(R.id.viewPager1);
        layout1 = (LinearLayout) findViewById(R.id.ll1);
        layout2 = (LinearLayout) findViewById(R.id.ll2);
        layout3 = (LinearLayout) findViewById(R.id.ll3);
        layout4 = (LinearLayout) findViewById(R.id.ll4);
        imageView1 = (ImageView) findViewById(R.id.img1);
        imageView2 = (ImageView) findViewById(R.id.img2);
        imageView3 = (ImageView) findViewById(R.id.img3);
        imageView4 = (ImageView) findViewById(R.id.img4);
        textView1 = (TextView) findViewById(R.id.txt1);
        textView2 = (TextView) findViewById(R.id.txt2);
        textView3 = (TextView) findViewById(R.id.txt3);
        textView4 = (TextView) findViewById(R.id.txt4);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                TabChange(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
    }

    private void initData(){
        list = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view1 = inflater.inflate(R.layout.table01, null);
        list.add(view1);
        Button button = view1.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "滑动效果的第一个界面", Toast.LENGTH_SHORT).show();
            }
        });
        View view2 = inflater.inflate(R.layout.table02, null);
        list.add(view2);
        View view3 = inflater.inflate(R.layout.table03, null);
        list.add(view3);
        View view4 = inflater.inflate(R.layout.table04, null);
        list.add(view4);
        MyPageAdapte myPageAdapte = new MyPageAdapte(list);
        viewPager.setAdapter(myPageAdapte);
        imageView1.setSelected(true);
        textView1.setSelected(true);
        currentImageView = imageView1;
        currentTextView = textView1;
    }

    @Override
    public void onClick(View v) {
        TabChange(v.getId());
    }

    private void TabChange(int position) {
        currentImageView.setSelected(false);
        currentTextView.setSelected(false);
        switch (position){
            case 0:
            case R.id.ll1:
                imageView1.setSelected(true);
                textView1.setSelected(true);
                viewPager.setCurrentItem(0);
                currentImageView = imageView1;
                currentTextView = textView1;
                break;
            case 1:
            case R.id.ll2:
                imageView2.setSelected(true);
                textView2.setSelected(true);
                viewPager.setCurrentItem(1);
                currentImageView = imageView2;
                currentTextView = textView2;
                break;
            case 2:
            case R.id.ll3:
                imageView3.setSelected(true);
                textView3.setSelected(true);
                viewPager.setCurrentItem(2);
                currentImageView = imageView3;
                currentTextView = textView3;
                break;
            case 3:
            case R.id.ll4:
                imageView4.setSelected(true);
                textView4.setSelected(true);
                viewPager.setCurrentItem(3);
                currentImageView = imageView4;
                currentTextView = textView4;
                break;
            default:
                break;
        }
    }
}
